package cc.jethro.jdxyq.homeFunctionRecycler;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import cc.jethro.jdxyq.R;
import cc.jethro.jdxysdk.ImgUtil.ImgLoader;

/**
 * 首页功能入口的recycler适配器
 * Created by jethr on 2017/5/11.
 */

public class HomeEntranceAdapter extends RecyclerView.Adapter<HomeEntranceAdapter.ViewHolder> {


    //要绑定到视图上的数据的合集
    private List<HomeEntrance> homeEntranceList ;
    private Context context;

    public HomeEntranceAdapter(Context context ,List<HomeEntrance> homeEntrances){
        this.context = context;
        this.homeEntranceList = homeEntrances;
    }

    /**
     * 为recycler添加item的布局
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).
                inflate(R.layout.item_function_entrance, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    /**
     * 把数据绑定到recycler的item上
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        HomeEntrance homeEntrance = homeEntranceList.get(position);
        ImgLoader.INSTANCE.loadImg(context,homeEntrance.getImage(),holder.imageView);
      //  Glide.with(context).load(homeEntrance.getImage()).into(holder.imageView);
        holder.textView.setText(homeEntrance.getTitle());
    }

    /**
     * 获取一共有多少个item
     * @return
     */
    @Override
    public int getItemCount() {
        return homeEntranceList.size();
    }

    /**
     * 初始化recycler里面的item的view的类
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView ;
        TextView textView ;

        //初始化view
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.item_function_icon);
            textView = (TextView) itemView.findViewById(R.id.item_function_title);
        }
    }
}
